import React from 'react'
import { useHistory } from 'react-router-dom'
// ================================================================ Image
import Bcimg from '@img/knowledge/bcimg.png'
/* --------------------------- View --------------------------- */
const List = ({name,label,model,onClick,value,type,bool,tips}) =>{
	return(
		<div className='fxm bbor1 lh22 g6 ' >
			<h6>{name}</h6>
			<div className='ex ml5 h'>
				{
					type ==='select'?
					<select placeholder='请输入' type='select' className='w h50' defaultValue={String(value)} style={{paddingLeft:'85%',border:0,outline:'none',background:'transparent'}} onChange={(e)=>{
						model.current[label] = e.target.value
					}}>
						<option className='tr' style={{direction:'ltr'}} value='1'>男</option>
						<option className='tr' style={{direction:'ltr'}} value='2'>女</option>
					</select>
					:<input placeholder={tips} className='w tr h50' readOnly={bool}  defaultValue={value} onChange={(e)=>{
						model.current[label] = e.target.value
						onClick?.(e.target.value)
					}} />
				}
			</div>
		</div>
	)
}
const Index = ( ) => {
	const model = React.useRef({
		sex:'1',
	})
	const [ ajax, setAjax ] = React.useState({ data:{} })
	const [ key, setKey ] = React.useState( 0 )
	const param = window.$fn.query()
	const history = useHistory( )
	React.useEffect(()=>{
		const param = window.$fn.query()
		model.current.spec_code = param.spec_code
		window.$http.pull(setAjax,'case/info',{ param, loading:true }).then(data=>{
			// history.replace('/ewm?spec_code='+ data?.case?.spec_code)
			if(data.case){
				model.current.patient_name = data.case.patient_name
				model.current.sex = data.case.sex ?? '1'
				model.current.age = data.case.age
				model.current.id_card = data.case.id_card
				model.current.phone = data.case.phone
				model.current.remark = data.case.remark
				model.current.kind_code = data.case.kind_code
				setKey(key=>key + 1)
			}
			// if(data?.finished===1){
			// 	history.replace('/ewm?spec_code='+ data?.case?.spec_code)
			// }else{
			// 	if(data.case.sex){
			// 		model.current.patient_name = data.case.patient_name
			// 		model.current.sex = data.case.sex
			// 		model.current.age = data.case.age
			// 		model.current.id_card = data.case.id_card
			// 		model.current.phone = data.case.phone
			// 		model.current.remark = data.case.remark
			// 		setKey(key=>key + 1)
			// 	}
			// }
		})
	}, [ history ])
	return (
		<>
			<div className='wh plr10 fv' style={{background:`url(${Bcimg}) no-repeat top center/100%`, paddingTop:'42%'}}>
				<div className='ex rel'>
					<div className='abs_full bcf r8px p10 oys' key={key} >
						<div className='fx'>
							<h5 className='b' style={{letterSpacing:'2px'}}>项目:</h5>
							<p className='b' style={{letterSpacing:'1px'}}>{ajax.data?.kind?.kind_name}</p>
						</div>
						<List name='条码号' model={model} label='spec_code' bool={true} value={param.spec_code} onClick={ spec_code =>{
							if(spec_code===''){
								
							}else{
								window.$http.pull(setAjax,'case/info',{ param:{ spec_code }, loading:true }).then(data=>{
									model.current.kind_code = data.case.kind_code
								})
							}
						}} />
						<List name='姓名' tips='请输入' model={model} label='patient_name' value={ajax.data?.case?.patient_name} />
						<List name='性别' tips='请输入' model={model} label='sex' type='select' value={ajax.data?.case?.sex} />
						<List name='年龄' tips='请输入' model={model} label='age' value={ajax.data?.case?.age} />
						<List name='身份证号' tips='请输入' model={model} label='id_card' value={ajax.data?.case?.id_card} />
						<List name='手机号' tips='请输入' model={model} label='phone' value={ajax.data?.case?.phone} />
						<div className='mt15 r5px lh22'><textarea placeholder='请输入备注信息' className='bcb wh p10' defaultValue={ajax.data?.case?.remark} style={{resize:'none',border:'none',height:150}}
							onChange={(e)=>{
								model.current.remark = e.target.value
							}}
						 /></div>
					</div>
				</div>
				<div className='fxmc ptb15'>
					<button className='cf bcm r8px h40 f15' style={{width:'120px'}} onClick={()=>{
						window.$http.post(null,'case/save',{param:model.current,loading:true}).then(()=>{
							window.$fn.toast('保存信息成功')
							history.push(`/ewm?spec_code=${model.current.spec_code}`)
						})
					}}>确定</button>
					<button className='cf bcm r8px h40 f15 ml20' style={{width:'120px'}} onClick={()=>{
						history.push(`/ewm?spec_code=${model.current.spec_code}`)
					}}>跳过</button>
				</div>
			</div>
		</>
	)
}
export default Index